package edu.faytechcc.csc.gui;

import edu.faytechcc.csc.messages.Connection;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * Please describe what this class does here.
 *
 * CSC 251 0001 DATE:
 *
 * @author Stephanie Mckenzie
 */
public class LogInFrame extends JFrame
{

    private Connection conn;
    private JTextField nameTextField;
    private JPasswordField passwordField;
    private String name;

    public LogInFrame(Connection conn)
    {
        this.conn = conn;
    }

    public void createAndShowGUI()
    {
        //  conn = new Connection();
        JPanel mainPanel = new JPanel();
        JPanel userNamePanel = new JPanel();
        JPanel passwordPanel = new JPanel();
        JPanel buttonPanel = new JPanel();
        JLabel nameLabel = new JLabel("User Name:");
        JLabel passwordLabel = new JLabel("Password:");
        nameTextField = new JTextField(15);
        passwordField = new JPasswordField(15);
        JButton logInButton = new JButton("Log In");

        //change the text below to set the Window title
        this.setTitle("Log In");

        //Close the Window when X is clicked
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        //Layouts
        this.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        mainPanel.setLayout(new GridLayout(3, 1));
        userNamePanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
        passwordPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

        //add things
        this.add(mainPanel);

        mainPanel.add(userNamePanel);
        mainPanel.add(passwordPanel);
        mainPanel.add(buttonPanel);

        userNamePanel.add(nameLabel);
        userNamePanel.add(nameTextField);

        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);

        buttonPanel.add(logInButton);
        logInButton.addActionListener(new logInButtonListener());

        //Display the window.
        this.pack();
        this.setAutoRequestFocus(true);
        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }

    /**
     * @return the name
     */
    @Override
    public String getName()
    {
        return name;
    }

    private class logInButtonListener implements ActionListener
    {

        char[] password;

        @Override
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                name = nameTextField.getText();
                password = passwordField.getPassword();
                if (!name.equals("") && !(password.length == 0))
                {
                    conn.logIn(name, password);

                    //clear passwordField
                    passwordField.setText(null);

                    //you have to hit log in again after getting the correct message
                    if (conn.isLogged())
                    {
                        dispose();
                    } else
                    {
                        nameTextField.setBackground(Color.red);
                        nameTextField.setForeground(Color.white);
                        passwordField.setBackground(Color.red);
                        passwordField.setForeground(Color.white);
                    }
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Please enter a "
                            + "Name and Password");
                }

            } catch (Exception ex)
            {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }
    }
}
